package org.project.product.android.pfe_fanta;

import org.project.product.android.pfe.fanta.R;
import org.project.product.android.pfe.fanta.adapter.DataManagerFacade;
import org.project.product.android.pfe.fanta.adapter.ProductListAdapter;
import org.project.product.android.pfe.fanta.domain.Category;
import org.project.product.android.pfe.fanta.domain.Product;
import org.project.product.android.pfe.fanta.util.Utils;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

public class ProductListActivity extends Activity {

	private Category categorySelected;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		this.setContentView(R.layout.activity_list_product);
		categorySelected = (Category) getIntent().getExtras().getSerializable(
		        Utils.CATEGORY_SELECTED);
		((TextView) findViewById(R.id.title)).setText(categorySelected
		        .getName());
		initViewAnListen();

	}

	@Override
	protected void onResume() {
		super.onResume();
		initViewAnListen();
	}

	private void initViewAnListen() {
		ListView listView = (ListView) findViewById(R.id.list_product);
		final ProductListAdapter adapter = new ProductListAdapter(
		        DataManagerFacade.getInstance(this).getProductByCategory(
		                categorySelected));
		listView.setAdapter(adapter);
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
			        long arg3) {
				Product product = (Product) adapter.getItem(arg2);
				Log.i("Product", product.toString());
				Utils.lancerActivity(ProductListActivity.this,
				        ProductViewActivity.class, product);

			}
		});
	}

	public void addProduct(View v) {
		Utils.lancerActivity(this, AddProduct.class, categorySelected);
	}

	public void editCategory(View v) {
		Utils.lancerActivity(this, EditOrDeleteCategoryActivity.class,
		        categorySelected);
	}

}
